package edu.gatech.cs2340.mule.view;

import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

import edu.gatech.cs2340.mule.model.Player;
import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;

/**
 * This panel holds the scores of each player and ranks them in order
 * @author Squib
 *
 */
public class ScorePanel extends JPanel {
	
	private JLabel p1Label, p2Label, p3Label, p4Label;
	private JLabel p1Score, p2Score, p3Score, p4Score;
	private JLabel l3, l4;
	
	/**
	 * Initialize the panel.
	 */
	public ScorePanel() {
		GridLayout layout = new GridLayout(4, 3);
		setLayout(layout);
		setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		
		JLabel l1 = new JLabel("  1");
		l1.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		l1.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(l1);
		
		p1Label = new JLabel("");
		p1Label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p1Label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p1Label);
		
		p1Score = new JLabel();
		p1Score.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p1Score.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p1Score);
		
		//-----------------------------------------------------------------
		
		JLabel l2 = new JLabel("  2");
		l2.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		l2.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(l2);
		
		p2Label = new JLabel("");
		p2Label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p2Label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p2Label);
		
		p2Score = new JLabel();
		p2Score.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p2Score.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p2Score);
		
		//-----------------------------------------------------------------
		
		l3 = new JLabel("  3");
		l3.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		l3.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(l3);
		
		p3Label = new JLabel("");
		p3Label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p3Label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p3Label);
		
		p3Score = new JLabel();
		p3Score.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p3Score.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p3Score);
		
		//-----------------------------------------------------------------
		
		l4 = new JLabel("  4");
		l4.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		l4.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(l4);
		
		p4Label = new JLabel("");
		p4Label.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p4Label.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p4Label);
		
		p4Score = new JLabel();
		p4Score.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		p4Score.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		add(p4Score);
		
	}
	
	/**
	 * Updates the scoreboard based on the current order of the players
	 * @param players the current players in order from lowest score to highest
	 */
	public void updateScoreboard(Player[] players) {
		p1Label.setText(players[players.length - 1].getPlayerName() + "  ");
		p1Score.setText("  " + players[players.length - 1].calculatePlayerScore());
		p1Label.setForeground(players[players.length - 1].getPlayerColor());
		p1Score.setForeground(players[players.length - 1].getPlayerColor());
		
		p2Label.setText(players[players.length - 2].getPlayerName() + "  ");
		p2Score.setText("  " + players[players.length - 2].calculatePlayerScore());
		p2Label.setForeground(players[players.length - 2].getPlayerColor());
		p2Score.setForeground(players[players.length - 2].getPlayerColor());
		
		if(players.length > 2) {
			p3Label.setText(players[players.length - 3].getPlayerName() + "  ");
			p3Score.setText("  " + players[players.length - 3].calculatePlayerScore());
			p3Label.setForeground(players[players.length - 3].getPlayerColor());
			p3Score.setForeground(players[players.length - 3].getPlayerColor());
		}
		else {
			l3.setText("");
		}
		
		if(players.length > 3) {
			p4Label.setText(players[players.length - 4].getPlayerName() + "  ");
			p4Score.setText("  " + players[players.length - 4].calculatePlayerScore());
			p4Label.setForeground(players[players.length - 4].getPlayerColor());
			p4Score.setForeground(players[players.length - 4].getPlayerColor());
		}
		else {
			l4.setText("");
		}
	} //end method updateScoreboard
}
